package com.car.dao.impl;

import com.car.dao.PurchaseRecordDao;
import com.car.domain.PurchaseRecord;
import com.car.utils.JDBCUtils;

import java.util.List;

public class PurchaseRecordImpl implements PurchaseRecordDao {
    @Override
    public int add(PurchaseRecord pr) throws Exception {
        String sql="insert into t_purchase_record(user_id,car_id,employee_id,purchase_datetime)"+
                " values(?,?,?,?)";
        return JDBCUtils.update(sql,pr.getUserId(),pr.getCarId(),pr.getEmployeeId(),pr.getPurchaseDate());
    }

    @Override
    public int delete(int id) throws Exception {
        String sql="delete from t_purchase_record where id=?";
        return JDBCUtils.update(sql, id);
    }

    /**
     * 修改
     *
     * @param pr 实体类
     * @return 影响记录数
     */
    @Override
    public int update(PurchaseRecord pr) throws Exception {
        String sql="update t_purchase_record set user_id=?,car_id=?,employee_id=?,purchase_datetime=? " +
                "where id=?";
        return JDBCUtils.update(sql,pr.getUserId(),pr.getCarId(),pr.getEmployeeId(),pr.getPurchaseDate(),pr.getId());
    }
    @Override
    public PurchaseRecord queryById(int id) throws Exception {
        String sql="select * from t_purchase_record where id=?";
        return JDBCUtils.query(sql,PurchaseRecord.class,id);
    }

    @Override
    public List<PurchaseRecord> queryAll() throws Exception {
        String sql="select * from t_purchase_record";
        return JDBCUtils.querylist(sql,PurchaseRecord.class);
    }

    @Override
    public List<PurchaseRecord> findByUserId(Integer userId) throws Exception {
        String sql="select * from t_purchase_record where user_id=?";
        return JDBCUtils.querylist(sql,PurchaseRecord.class,userId);
    }
}
